<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Category;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;

class CategoryController extends AdminController
{
    public string $create_permission_name = 'category-create';
    public string $edit_permission_name = 'category-edit';
    public string $delete_permission_name = 'category-delete';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Category(), function (Grid $grid) {
            $grid->model()->orderBy('sort');
            $grid->column('id');
            $grid->column('name');
            if ($this->edit && Admin::user()->can($this->edit_permission_name) && !$this->isTrashed()) {
                $grid->column('show')->switch();
            } else {
                $grid->column('show')->bool();
            }
            $grid->column('sort');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');
                $filter->equal('show')->select(getBoolOption());
            });
            $this->gridCommon($grid);
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new Category(), function (Form $form) {
            $form->text('name');
            $form->switch('show')->default(true);
            $form->number('sort')->default(100)->min(1)->max(100);
        });
    }
}
